package teste;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;

public class MySAXApp extends DefaultHandler {

	public static void main(String args[]) throws Exception {
		
		//String dirFonteArqXML = args[0];
		String arquivo1 = "d:\\XML\\teste.xml";
		//String arquivo1 = "207.xml";
		
		XMLReader xr = XMLReaderFactory.createXMLReader();
		MySAXApp handler = new MySAXApp();
		xr.setContentHandler(handler);
		xr.setErrorHandler(handler);

		// Parse each file provided on the
		// command line.
		
		FileReader r = new FileReader("teste.xml");
		//xr.parse(new InputSource(r));
		
		
		
		FileReader reader = null;
		try {
			reader = new FileReader(new File(arquivo1));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.out.println("Opa");
		}
		BufferedReader leitor = new BufferedReader(reader);
		String str;
		while((str = leitor.readLine()) != null){
			str = str.trim().replaceFirst("﻿<","<");
			
	          System.out.println(str);
	       }
		//xr.pa
		xr.parse(str);
		

		/*
		 * for (int i = 0; i < args.length; i++) { FileReader r = new
		 * FileReader("teste.xml"); 
		 * xr.parse(new InputSource(r));
		 * System.out.println("aqui!!!!"); }
		 */
	}

	public MySAXApp() {
		super();
	}

	// //////////////////////////////////////////////////////////////////
	// Event handlers.
	// //////////////////////////////////////////////////////////////////

	public void startDocument() {
		System.out.println("Start document");
	}

	public void endDocument() {
		System.out.println("End document");
	}

	public void startElement(String uri, String name, String qName,
			Attributes atts) {

		if ("".equals(uri))
			System.out.println("Start element: " + qName);
		else
			//System.out.println("Start element: {" + uri + "}" + name);
			System.out.println("Start element: "  + name);
	}

	public void endElement(String uri, String name, String qName) {
		if ("".equals(uri))
			System.out.println("End element: " + qName);
		else
			//System.out.println("End element:   {" + uri + "}" + name);
			System.out.println("End element:   " + name);
	}

	public void characters(char ch[], int start, int length) {
		String teste = "";
		System.out.print("Characters:    \"");
		for (int i = start; i < start + length; i++) {
			switch (ch[i]) {
			case '\\':
				System.out.print("\\\\");
				break;
			case '"':
				System.out.print("\\\"");
				break;
			case '\n':
				System.out.print("\\n");
				break;
			case '\r':
				System.out.print("\\r");
				break;
			case '\t':
				System.out.print("\\t");
				break;
			default:
				teste = teste+""+ch[i];
				//System.out.print(ch[i]);
				break;
			}
		}
		System.out.print(teste);
		System.out.print("\"\n");
	}

}
